/**
 * Created by wsx on 2017/2/1.
 */
$(function () {
    $(function(){
        var websocket;
        if ('WebSocket' in window) {
            alert("WebSocket");
            websocket = new WebSocket("ws://localhost:8080/echo");
        } else if ('MozWebSocket' in window) {
            alert("MozWebSocket");
            websocket = new MozWebSocket("ws://echo");
        } else {
            alert("SockJS");
            websocket = new SockJS("http://localhost:8080/sockjs/echo");
        }
        websocket.onopen = function (evnt) {
            $("#tou").html("链接服务器成功!")
        };
        websocket.onmessage = function (evnt) {
            $("#msg").html($("#msg").html() + "<br/>" + evnt.data);
        };
        websocket.onerror = function (evnt) {
            alert("error");
        };
        websocket.onclose = function (evnt) {
            $("#tou").html("与服务器断开了链接!")
        }
        $('#send').bind('click', function() {
            send();
        });
        function send(){
            if (websocket != null) {
                var message = document.getElementById('message').value;
                websocket.send(message);
            } else {
                alert('未与服务器链接.');
            }
        }
    });
});